package com.mo.lib.view.textview

import androidx.core.content.ContextCompat

import androidx.annotation.ColorRes

import androidx.annotation.StringRes

import android.text.Spanned

import android.text.style.ForegroundColorSpan

import android.text.SpannableString

import android.text.TextUtils

import android.content.Context

import android.content.res.TypedArray

import android.util.AttributeSet

import com.mo.lib.R


/**
 * @ author：mo
 * @ data：2022/4/6:16:52
 * @ 功能：
 */
class SuperTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : androidx.appcompat.widget.AppCompatTextView(context, attrs, defStyleAttr) {
    private var mDuration = 200
    private var mIsStart = false
    private var mText: CharSequence? = null
    private var mPosition = 0
    private var mSelectedColor = -0xff01
    private var mOnDynamicListener: OnDynamicListener? = null
    private var mDynamicStyle = DynamicStyle.NORMAL

    init {
        val a: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.KSuperTextView)
        mText = a.getText(R.styleable.KSuperTextView_dynamicText)
        mDuration = a.getInt(R.styleable.KSuperTextView_duration, mDuration)
        mSelectedColor = a.getColor(R.styleable.KSuperTextView_selectedColor, mSelectedColor)
        mDynamicStyle = DynamicStyle.getFromInt(a.getInt(R.styleable.KSuperTextView_dynamicStyle, 0))
        a.recycle()
    }


    fun start() {
        if (mIsStart) {
            return
        }
        if (TextUtils.isEmpty(mText)) { //如果动态文本为空、则取getText()的文本内容
            mText = text
        }
        mPosition = 0
        if (!TextUtils.isEmpty(mText)) {
            mIsStart = true
            post(mRunnable)
        } else {
            mIsStart = false
            if (mOnDynamicListener != null) {
                mOnDynamicListener!!.onCompile()
            }
        }
    }

    private val mRunnable: Runnable = object : Runnable {
        override fun run() {
            if (DynamicStyle.TYPEWRITING == mDynamicStyle) {
                text = mText!!.subSequence(0, mPosition)
            } else if (DynamicStyle.CHANGE_COLOR == mDynamicStyle) {
                setChangeColorText(mPosition)
            } else {
                text = mText
                mIsStart = false
                if (mOnDynamicListener != null) {
                    mOnDynamicListener!!.onCompile()
                }
                return
            }
            if (mPosition < mText!!.length) {
                if (mOnDynamicListener != null) {
                    mOnDynamicListener!!.onChange(mPosition)
                }
                mPosition++
                postDelayed(this, mDuration.toLong())
            } else {
                if (mOnDynamicListener != null) {
                    mOnDynamicListener!!.onChange(mPosition)
                }
                mIsStart = false
                if (mOnDynamicListener != null) {
                    mOnDynamicListener!!.onCompile()
                }
            }
        }
    }

    private fun setChangeColorText(position: Int) {
        val spannableString = SpannableString(mText)
        spannableString.setSpan(ForegroundColorSpan(mSelectedColor), 0, position, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
        text = spannableString
    }

    fun setDynamicText(@StringRes resId: Int) {
        mText = resources.getText(resId)
    }

    fun setDynamicText(text: CharSequence?) :SuperTextView{
        mText = text
        return this
    }

    fun getDynamicText(): CharSequence? {
        return mText
    }

    fun setDuration(duration: Int):SuperTextView {
        mDuration = duration
        return this
    }

    fun getDuration(): Int {
        return mDuration
    }

    fun getDynamicStyle(): DynamicStyle {
        return mDynamicStyle
    }

    fun setDynamicStyle(dynamicStyle: DynamicStyle):SuperTextView {
        mDynamicStyle = dynamicStyle
        return this
    }

    fun setOnDynamicListener(onDynamicListener: OnDynamicListener?) :SuperTextView{
        mOnDynamicListener = onDynamicListener
        return this
    }

    fun setSelectedColor(selectedColor: Int):SuperTextView {
        mSelectedColor = selectedColor
        return this
    }

    fun setSelectedColorResource(@ColorRes resId: Int):SuperTextView {
        mSelectedColor = ContextCompat.getColor(context, resId)
        return this
    }

    interface OnDynamicListener {
        fun onChange(position: Int)
        fun onCompile()
    }
}

enum class DynamicStyle(private val mValue: Int) {
    NORMAL(0),
    TYPEWRITING(1),
    CHANGE_COLOR(2);

    companion object {
        fun getFromInt(value: Int): DynamicStyle {
            for (style in values()) {
                if (style.mValue == value) {
                    return style
                }
            }
            return NORMAL
        }
    }
}